home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- #
- # Apple Developer Technical Support
- #
- # MungeDeamon
- # A small faceless background-only application for looking at other folks resources
- #
- # MungeDeamonMain.c - C Source
- #
- # Copyright © 1992 Apple Computer, Inc.
- # All rights reserved.
- #
- # Versions:
- # 1.0 12/92 C.K. Haun <TR>
- #
- #
- # This is a faceless background process who's whole mission in
- # life is to open resource forks, get information from them, and
- # pass that information back to whoever asked for it.
- # I wrote it to show one handy use of a background-only application,
- # manipulating other people's resource forks in your application can often
- # cause you a lot of problems, you can inadvertantly load one of
- # their resources instead of your own (bad, imagine running someone elses CODE
- # segment instead of what you expected). Also, you may not be sure
- # if you can close the resource fork after you opened it (closing the resource fork
- # of a running application is Not Good), and that means that their resource map
- # will forever live in your heap, taking up memory and confusion.
- # Using MungeDeamon to do the work for you gets rid of most of these problems.
- # First, since MungeDeamon only has one resource it cares about, it's own MAIN
- # code resource which is _always_ in memory, you don't need to worry about
- # accidentally loading any resources.
- # Second, any extra memory use is in MungeDeamon's heap, not the main app.
- # Third, I have a flag in the request that can tell MungeDeamon to
- # quit as soon as it's done, letting the Process Manager handle closing forks
- # and also immediatly cleaning up any memory needed in this operation.
- # And so on, there are lots of handy things you can do with a background-only app.
- #
- # The main things to note are....
- # 1) Look at the SIZE resource for the flag settings you'll
- # need to let the Finder™ know that you only want to work
- # in the background.
- # 2) Notice that you really do have your own heap and your own
- # event loop. You can do anything a foreground application
- # can do, send AppleEvents, PPC stuff, anything else you'd
- # like EXCEPT a graphic interface.
- # 3) NOTE that no managers are started up. You cannot start up
- # Window, Menu, Dialogs, or anything else that
- # deals with the graphic interface. Just leave them out.
- # You CAN start up QuickDraw if you want to do some graphic
- # processing, but DO NOT draw to the screen, only use things
- # like offscreen grafports or GWorlds. Or you may
- # want to start it to use Random().....
- #
- # Of course, a backgrounder can be launched from the Finder™
- # with a double-click. However, if you'd like the backgrounder
- # to perform some useful service for your main application, driver,
- # DA, or whatever, you will want it running all the time.
- # The BEST way to insure this is to install the backgrounder in the
- # StartUp Items folder in the system folder. This will insure that
- # it is always launched, and it will also reduce memory fragmentation
- # since it will be installed at startup time. You can search for it
- # when you need it with IPCListPorts (see the PPC toolbox documentation
- # or the AECDEV code).
- # Optionally, you can use the LaunchApplication trap to launch it
- # when you need it, and kill it when you're done.
- # And remember, it's a backgrounder, you can't see it. To kill it
- # use TaskIt or ProcDoggie.
- #
- # ---------------------------------------------------------------
- # Use this sample as a starting point, and adapt its' routines to
- # meet the specific needs of your project.
- # This sample will grow as more edition types are implemented,
- # periodically check AppleLink for a later, expanded sample.
- # This application is an example of the form of a Macintosh
- # application; it is NOT a template. It is NOT intended to be
- # used as a foundation for the next world-class, best-selling,
- # 600K application. A stick figure drawing of the human body may
- # be a good example of the form for a painting, but that does not
- # mean it should be used as the basis for the next Mona Lisa.
- #
- #
- ------------------------------------------------------------------------------*/
-
-
- #include "MungeDeamon.h"
- Boolean gQuit = false;
- EventRecord ERecord;
-
- Boolean gHasAppleEvents;
- unsigned long gMySleep = 200; /* Long time. Change this if */
- /* you'd like to do null processing. Just keep in mind that the */
- /* user does NOT know that you exisist, and if you are eating */
- /* up a bunch of time the user will not know why his or her */
- /* machine is slowing down. */
- unsigned long quitTime;
- ProcessSerialNumber myPSN;
- void SetQuitTime(void);
-
- short gOurResRef; /* our resource file reference number */
-
- main()
- {
- /* We are NOT initializing any managers other than QuickDraw. We're in the background, with no */
- /* face, we can't use windows or dialogs or menus. If you need to talk to the */
- /* user you can post a notification, or launch an application to comunicate */
- /* Passing an AppleEvent in the launchapplication trap could do the */
- /* communication for you. */
- /* no nothing but events */
- /* Why am I calling InitGraf here? Well, I'm a background-only */
- /* app, but I use the AppleEvent manager and ask it to auto-generate */
- /* return and transaction IDs. AppleEvent Manager uses Random() to */
- /* do this, so I have to have QD started */
- gOurResRef = CurResFile();
-
- UnloadSeg((Ptr)_DataInit); /* throw out setup code */
-
- MaxApplZone();
- InitGraf((Ptr)&qd.thePort);
- InitAEStuff();
- GetCurrentProcess(&myPSN);
- /* no nothing but high level events */
- while (gQuit == false) {
- WaitNextEvent(highLevelEventMask, &ERecord, gMySleep, 0);
- /* if any event other than a nul happens, reset the timeout */
- if(ERecord.what !=nullEvent)SetQuitTime();
-
- if (ERecord.what == kHighLevelEvent)
- DoHighLevel(&ERecord);
- if(quitTime < TickCount())gQuit = true;
- }
- }
- /* I hate having to kill deamons all the time, so this one */
- /* self-destructs after 10 minutes of non-use */
- void SetQuitTime(void)
- {
- quitTime = TickCount() + ( 60 * 60 * 10 );
-
-
- }
-
-
-